package com.cloud.controller;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author tangyuan2meicloud.com
 * @DATE 2021/6/24 6:55
 * @Desc
 * @Remarks
 */
@RestController
public class DiscoveryController {
    @Resource
    private DiscoveryClient discoveryClient;

    @RequestMapping("/discovery")
    public void discovery() {
        List<String> services = discoveryClient.getServices();
        services.stream().forEach(System.out::println);

        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PRODUCER");
        instances.stream().forEach(serviceInstance -> {
            System.out.println(serviceInstance.getServiceId() + "\t" + serviceInstance.getPort() + "\t" + serviceInstance.getUri());
        });
    }
}
